package com.spider.gp.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//import com.hankcs.lucene.HanLPAnalyzer;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class FileSimilarity {
    private static Analyzer analyzer = new StandardAnalyzer();

    private static String preprocessText(String text) {
        // 去除数字、标点符号等无意义字符
        text = text.replaceAll("\\d+", "");
        text = text.replaceAll("[\\pP\\p{Punct}]", "");
        text = text.trim().toLowerCase();
        return text;
    }

//    public static Map<String, Double> getTFIDFMap(String text) throws IOException {
//        Directory directory = FSDirectory.open(new File("index").toPath());
//        IndexWriterConfig config = new IndexWriterConfig(analyzer);
//        IndexWriter writer = new IndexWriter(directory, config);
//
//        Document doc = new Document();
//        doc.add(new Field("content", text, Field.Store.YES, Field.Index.ANALYZED));
//        writer.addDocument(doc);
//
//        writer.close();
//
//        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("index").toPath()));
//        IndexSearcher searcher = new IndexSearcher(reader);
//
//        String[] terms = text.split(" ");
//        Map<String, Double> tfidfMap = new HashMap<>();
//        for (String term : terms) {
//            TermQuery query = new TermQuery(new Term("content", term));
//            ScoreDoc[] hits = searcher.search(query, 1).scoreDocs;
//            double tf = hits.length;
//            double idf = Math.log((double)reader.numDocs() / (double)searcher.getIndexReader().docFreq(new Term("content", term)) + 1.0);
//            tfidfMap.putIfAbsent(term, 0.0);
//            tfidfMap.put(term, tfidfMap.get(term) + tf * idf);
//        }
//
//        return tfidfMap;
//    }

    public static double getCosineSimilarity(Map<String, Double> vector1, Map<String, Double> vector2) {
        double dotProduct = 0.0;
        double norm1 = 0.0;
        double norm2 = 0.0;

        for (String key : vector1.keySet()) {
            if (key.equals("")){
                continue;
            }
            if (vector2.containsKey(key)) {
                dotProduct += vector1.get(key) * vector2.get(key);
            }
            norm1 += Math.pow(vector1.get(key), 2);
        }

        for (Double value : vector2.values()) {
            norm2 += Math.pow(value, 2);
        }

        return dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2));
    }

//    public static void main(String[] args) throws Exception {
//        // 读取PDF文件内容
//        PDDocument document1 = PDDocument.load(new File("D:\\51060000000002576001001\\四川省都江堰勘测设计院有限责任公司.pdf"));
//        PDFTextStripper stripper1 = new PDFTextStripper();
//        String content1 = stripper1.getText(document1);
//        document1.close();
//
//        PDDocument document2 = PDDocument.load(new File("D:\\51060000000002576001001\\南通市水利勘测设计研究院有限公司.pdf"));
//        PDFTextStripper stripper2 = new PDFTextStripper();
//        String content2 = stripper2.getText(document2);
//        document2.close();
//
//        // 预处理并转化为向量表示
//        Map<String, Double> vector1 = getTFIDFMap(preprocessText(content1));
//        Map<String, Double> vector2 = getTFIDFMap(preprocessText(content2));
//
//        // 计算文本相似度
//        double similarity = getCosineSimilarity(vector1, vector2);
//        System.out.println("文本相似度为：" + similarity);
//
//        // 删除临时索引文件
//        FileUtils.deleteDirectory(new File("index"));
//    }
}
